package com.device.service;

import com.device.entity.User;
import com.device.entity.UserCredentials;
import com.device.repository.UserRepository;
import com.device.repository.UserCredentialsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Optional;

@Service
public class UserService implements UserDetailsService {
    
    @Autowired
    private UserRepository userRepository;
    
    @Autowired
    private UserCredentialsRepository userCredentialsRepository;
    
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    
    public Optional<User> findByEmail(String email) {
        return userRepository.findByEmail(email);
    }
    
//    public boolean authenticateUser(String email, String password) {
//        Optional<User> userOpt = userRepository.findByEmail(email);
//        if (!userOpt.isPresent()) {
//            return false;
//        }
//
//        User user = userOpt.get();
//        Optional<UserCredentials> credentialsOpt = userCredentialsRepository.findByUserId(user.getId());
//        if (!credentialsOpt.isPresent()) {
//            return false;
//        }
//
//        UserCredentials credentials = credentialsOpt.get();
//        if (!credentials.getEnabled()) {
//            return false;
//        }
//
//        return passwordEncoder.matches(password, credentials.getPassword());
//    }
//
//    public void updateLastLoginTime(String email) {
//        Optional<User> userOpt = userRepository.findByEmail(email);
//        if (userOpt.isPresent()) {
//            User user = userOpt.get();
//            Optional<UserCredentials> credentialsOpt = userCredentialsRepository.findByUserId(user.getId());
//            if (credentialsOpt.isPresent()) {
//                UserCredentials credentials = credentialsOpt.get();
//                credentials.setLastLoginTs(System.currentTimeMillis());
//                credentials.setFailedLoginAttempts(0);
//                userCredentialsRepository.save(credentials);
//            }
//        }
//    }
//
//    public void incrementFailedLoginAttempts(String email) {
//        Optional<User> userOpt = userRepository.findByEmail(email);
//        if (userOpt.isPresent()) {
//            User user = userOpt.get();
//            Optional<UserCredentials> credentialsOpt = userCredentialsRepository.findByUserId(user.getId());
//            if (credentialsOpt.isPresent()) {
//                UserCredentials credentials = credentialsOpt.get();
//                credentials.setFailedLoginAttempts(credentials.getFailedLoginAttempts() + 1);
//                userCredentialsRepository.save(credentials);
//            }
//        }
//    }


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Optional<User> userOpt = userRepository.findByEmail(username);
        User user = userOpt.get();
        Optional<UserCredentials> credentialsOpt = userCredentialsRepository.findByUserId(user.getId());
        UserCredentials credentials = credentialsOpt.get();
        return new  org.springframework.security.core.userdetails.User(
                user.getEmail(),
                credentials.getPassword(),
                true,
                true,
                true,
                true,
                new ArrayList<>()
        );
    }
}